package com.hj.storage.controller;


import com.hj.exception.HjException;
import com.hj.exception.HjExceptionEnum;
import com.hj.security.domain.annotation.NoAuth;
import com.hj.hos.HosService;
import com.hj.storage.domain.vo.SysFileUploadVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author hj
 * @Date 2023/4/29
 * @Description: 文件上传
 */
@RestController
@RequestMapping("/file/upload")
@Tag(name = "文件上传")
@AllArgsConstructor
@NoAuth
public class StorageController {

    private final HosService storageService;

    @PostMapping("/one")
    @Operation(summary = "上传")
    public SysFileUploadVO upload(@RequestParam("file") MultipartFile file) throws Exception {
        if (file.isEmpty()) {
            throw new HjException(HjExceptionEnum.NO_FILE);
        }
        // 上传路径
        String path = storageService.getPath(file.getOriginalFilename());
        // 上传文件
        String url = storageService.upload(file.getBytes(), path);
        SysFileUploadVO vo = new SysFileUploadVO();
        vo.setUrl(url);
        vo.setSize(file.getSize());
        vo.setName(file.getOriginalFilename());
        vo.setPlatform(storageService.properties.getConfig().getType().name());
        return vo;
    }


}
